import { Component, OnInit }                  from '@angular/core';
import { NzMessageService, NzTabChangeEvent } from 'ng-zorro-antd';
import { ModalHelper, TitleService }          from '@delon/theme';
import { Observable }                         from 'rxjs';
import { ActivatedRoute }                     from '@angular/router';
import { copy }                               from '@delon/util';
import { Device }                             from "@shared/model/device-dto";
import { DeviceService }                      from "../device.service";
import { StoreService }                       from "../../store/store.service";
import { Store }                              from "@shared/model/store-dto";
import { STColumn, STReq }                    from "@delon/abc";
import { SFDateWidgetSchema }                 from "@delon/form";
import { channelStatus, logStatus }           from "../index";


@Component({
    selector   : 'app-manager-device-view',
    templateUrl: './view.component.html',
})
export class ManagerDeviceViewComponent implements OnInit
{
    id: string;
    device: Device;
    store$: Observable<Store>;
    stores$: Observable<Array<Store>>;
    isLoading$: Observable<boolean>;


    logUrl: string;

    logColumns: STColumn[] = [
        { title: '消息ID', index: 'messageId' },
        {
            title: '时间',
            type : 'date',
            index: 'generateTime',
            width: 260,
            sort : { default: 'descend', reName: { ascend: 'asc', descend: 'desc' } },
        },
        { title: '类别', index: 'msgType', type: 'tag', tag: logStatus },
    ];
    logSearchSchema = {
        properties: {
            fromDate: {
                type  : 'string',
                title : '开始时间',
                format: 'date',
                ui    : { widget: 'date', end: 'toDate' } as SFDateWidgetSchema,
            },
            toDate  : {
                type  : 'string',
                format: 'date',
                ui    : { widget: 'date', end: 'toDate' } as SFDateWidgetSchema,
            },
        },
    };
    req: STReq = {};


    page = { show: false, front: false };
    showStore = false;

    constructor(
        public msgSrv: NzMessageService,
        private titleService: TitleService,
        private route: ActivatedRoute,
        public devSrv: DeviceService,
        private modal: ModalHelper,
        public storeSrv: StoreService
    )
    {
        this.id = this.route.snapshot.params.id;

        this.devSrv.load(this.id).subscribe(value =>
        {
            this.device = value;
            if ( value.storeId )
                this.store$ = this.storeSrv.load(value.storeId);
            this.logUrl = `kx/api/devices/logs?deviceId=${ value.deviceId }`;
        });
        this.stores$ = storeSrv.loadAll();
    }

    ngOnInit(): void
    {
        // this.device$    = this.store.select(currentDevice).pipe(takeUntil(this.ngDestroy));
        // this.isLoading$ = this.store.select(deviceLoading).pipe(takeUntil(this.ngDestroy));
        // this.store.dispatch(SelectDevice(this.id));
        // this.device$.subscribe(value => {
        //   if (!value) this.store.dispatch(LoadDevice(this.id));
        // });
    }

    close()
    {
    }

    copy( content: string )
    {
        copy(content).then(value => this.msgSrv.info('复制成功'));
    }

    tabSelect( e: NzTabChangeEvent )
    {

    }

    editStore()
    {
        this.showStore = true
    }

    reload( e )
    {
        console.log(e);
        this.devSrv.update(this.device).subscribe(() =>
        {
            this.showStore = false;
            this.devSrv.load(this.id).subscribe(value =>
            {
                this.device = value;
                if ( value.storeId )
                    this.store$ = this.storeSrv.load(value.storeId)
            });
        })
    }

    handleCancel()
    {
        this.showStore = false
    }
}
